import {Component} from '@angular/core';
import { Storage } from '@ionic/storage';

import {
  ModalController,
  ViewController
} from 'ionic-angular';

import _ from 'lodash';
import {ContactChoosePage} from "../../../contact/choose/choose.component";
import {MarketingLinupListService} from "../list/list.service";

@Component({
  selector: 'page-marketing-lineup-query',
  templateUrl: './query.html',
  providers: [MarketingLinupListService]
})
export class MarketingLineupQueryPage {
	moa_page_name: any = "page-marketing-lineup-query";
  moa_page_explain: any = "任务管理 - 高级查询";

  condition = null;

  defaultEmpByTree = [];

  constructor(
              public modalCtrl: ModalController,
              public viewCtrl: ViewController,
              public storage: Storage,
              private listService: MarketingLinupListService,
              ) {

      this.initCondition();
      this.storage.get("advancedQueryCondition")
        .then(data => {
            console.log("test" + data);
            if (data != null) {
                this.condition = data;
            }
        });
      console.log(this.condition);
  }

  cancel() {
    this.viewCtrl.dismiss("close");
  }

  /**
   * [chooseEmp 选择人员]
   * @param {[type]} item [description]
   */
  chooseEmp() {
    console.log("##authorization.detail.chooseEmpnames 选择外出人员");
    let that = this;
    let modal = this.modalCtrl.create(ContactChoosePage, {
      selected: this.defaultEmpByTree,
      isSelectUser: true,
      isSelectOne: false
    });

    modal.present();
    modal.onDidDismiss((data: any[]) => {
      console.info(data);
      if (data) {
        that.defaultEmpByTree = data;
        let loginid = _.toString(_.map(data, "loginid"));
        that.condition.empName = _.toString(_.map(data, "name"));
        that.listService.getUsrIDFromOAServer(loginid).subscribe(usr_id => {
          that.condition.resourceid = "'" + _.toString(_.map(usr_id, "usr_id")) + "'";
        });
      }
    });
  }

  /**
   * 保存搜索条件并关闭Modal返回List页面加载搜索结果
   */
  search() {
      this.storage.set("advancedQueryCondition", this.condition);
      this.viewCtrl.dismiss("true");
  }

  /**
   * 清空查询条件
   */
  clear() {
      this.initCondition();
      this.storage.remove("advancedQueryCondition");
  }

  /**
   * 初始化查询条件
   */
  initCondition() {
    this.condition = {
      "taskname" : "",
      "taskstatus":"",
      "resourceid": "'ALL'",
      "empName": ""
    }
  }

}


